﻿
namespace MockingBird.Simulator.FunctionalTests.Common
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Web;
    using MockingBird.Simulator.Framework;
    using MockingBird.Simulator.Framework.Configuration;
    using Subtext.TestLibrary;

    public class TestHelper
    {
        public const string DEFAULT_PHYSICAL_APPLICATION_PATH = @"c:\inetpub\wwwroot\mockingbird";
        public const string DEFAULT_TARGET_URL = "http://localhost/mockingbird/customer.asmx?op=GetCustomer";
        public const string TARGET_URL_WITHOUT_QUERYSTRING = "http://localhost/mockingbird/customer.asmx";
        public const string DEFAULT_SOAP_ACTION = "http://www.contoso.com/services/customer/GetCustomer";

        public static void SetupSimulatedHttpEnvironment()
        {
            HttpSimulator simulator = new HttpSimulator("/", DEFAULT_PHYSICAL_APPLICATION_PATH);
            simulator.SetHeader("SOAPAction", DEFAULT_SOAP_ACTION);
            simulator.SimulateRequest(new Uri(DEFAULT_TARGET_URL));

        }

        public static void SetupContextWithSimulatedHttpEnvironment(string url, out ExecutionContext context, NameValueCollection headers)
        {
            //Setup a simulated Http environment

            HttpSimulator simulator = new HttpSimulator("/", DEFAULT_PHYSICAL_APPLICATION_PATH);
            if (headers.Count > 0)
            {
                foreach (string key in headers.Keys)
                {
                    simulator.SetHeader(key, headers.Get(key));
                }
            }
            simulator.SimulateRequest(new Uri(url));
            context = new ExecutionContext(GetAppConfigData(), HttpContext.Current);
        }

        public static void SetupContextWithSimulatedFormPost(string url, out ExecutionContext context)
        {
            //Setup a simulated Http environment

            HttpSimulator simulator = new HttpSimulator("/", DEFAULT_PHYSICAL_APPLICATION_PATH);
            simulator.SetHeader("SOAPAction", DEFAULT_SOAP_ACTION);
            simulator.SimulateRequest(new Uri(url), HttpVerb.POST);
            context = new ExecutionContext(GetAppConfigData(), HttpContext.Current);
        }

        public static void SetupContextRequestMessageAndFormat(string contentType, string requestMessage, ref ExecutionContext context)
        {
            context.ContentType = contentType;
            context.RequestMessage = requestMessage;
        }

        public static Dictionary<string, string> GetDefaultActionMap()
        {
            Dictionary<string, string> actionMap = new Dictionary<string, string>();
            actionMap.Add(DEFAULT_SOAP_ACTION, "GetCustomer");
            return actionMap;
        }

        public static Dictionary<string, string> GetDefaultRequestMap()
        {
            Dictionary<string, string> requestMap = new Dictionary<string, string>();
            requestMap.Add("dummy", "GetCustomer");
            return requestMap;
        }

        public static ApplicationConfiguration GetAppConfigData()
        {
            IConfigurationDataProvider configurationDataProvider = new NativeConfigurationDataProvider();
            ApplicationConfiguration configuration = new ApplicationConfiguration(configurationDataProvider);
            return configuration;

        }

    }
}
